package com.nj.redis.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
 /*
        当一个类实现了ApplicationContextAware接口之后，这个类就可以方便的获得ApplicationContext对象（Spring上下文），
        Spring发现某个Bean实现了ApplicationContextAware接口，
        Spring容器会在创建该Bean之后，自动调用该Bean的setApplicationContext（参数）方法，
        调用该方法时，会将容器本身ApplicationContext对象作为参数传递给该方法。
     */
public class SpringbootContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
    //springContext.getBean(Class type)可以获得容器中已注入的bean
    public Object getBean(Class type){
        return applicationContext.getBean(type);
    }
}
